#Region "using directives"
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports C4F.DevKit.Messaging
#End Region
Namespace MapiApplication
	Public Partial Class ProfileSettings
	Inherits Form
		#Region "Globals"
		Private mapiManager As MapiManager = New MapiManager()
		#End Region
		#Region "Constructors"
		Public Sub New()
			InitializeComponent()
			InitializePowerSettingsForm()
		End Sub
		#End Region
		#Region "Helper Methods"
		Private Sub InitializePowerSettingsForm()
			RenderAllProfilesIntoListBox()
			RenderAllProfilesIntoComboBox()
			If lbxAvailableProfiles.Items.Count <> 0 Then
				Me.btnRename.Enabled = True
				Me.btnRemove.Enabled = True
				Me.btnCopy.Enabled = True
			End If
		End Sub
		Private Sub RenderAllProfilesIntoComboBox()
			Dim availableProfiles As List(Of String) = New List(Of String)()
			Try
				cmbxGetAllProfiles.Items.Clear()
				availableProfiles = mapiManager.GetAvailableProfiles()
				For Each profile As String In availableProfiles
					cmbxGetAllProfiles.Items.Add(profile)
				Next
				'TODO: Known Issue: GetDefaultProfile is not implemented yet.
				'cmbxGetAllProfiles.Text = mapiManager.GetDefaultProfile();
				cmbxGetAllProfiles.SelectedIndex = 0
			Catch ex As MapiManagerException
				MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As NotImplementedException
				MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As Exception
				MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		Private Sub RenderAllProfilesIntoListBox()
			Dim availableProfiles As List(Of String) = New List(Of String)()
			Try
				lbxAvailableProfiles.Items.Clear()
				availableProfiles = mapiManager.GetAvailableProfiles()
				For Each profile As String In availableProfiles
					lbxAvailableProfiles.Items.Add(profile)
				Next
			Catch ex As MapiManagerException
				MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As Exception
				MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		Public ReadOnly Property lbxBoxSelectedValue() As String
			Get
				Return Me.lbxAvailableProfiles.SelectedItem.ToString()
			End Get
		End Property
		Private Sub SetProfile()
			Try
				If cmbxGetAllProfiles.Text <> String.Empty Then
					mapiManager.SetDefaultProfile(cmbxGetAllProfiles.Text)
					Main.profileName = cmbxGetAllProfiles.Text
				End If
			Catch ex As MapiManagerException
				MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As ArgumentNullException
				MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As ArgumentException
				MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As Exception
				MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		#End Region
		#Region "Event Handlers"
		Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
			Dim addProfile As AddProfile = New AddProfile()
			Dim dgResult As DialogResult = addProfile.ShowDialog()
            If dgResult = Windows.Forms.DialogResult.OK Then
                Try
                    mapiManager.CreateNewProfile(addProfile.TbxBoxValue)
                    RenderAllProfilesIntoListBox()
                    RenderAllProfilesIntoComboBox()
                    addProfile.Dispose()
                Catch ex As MapiManagerException
                    MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
                Catch ex As ArgumentNullException
                    MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
                Catch ex As ArgumentException
                    MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
                End Try
            Else
                addProfile.Dispose()
            End If
		End Sub
		Private Sub btnCopy_Click(ByVal sender As Object, ByVal e As EventArgs)
			If lbxAvailableProfiles.SelectedItem IsNot Nothing Then
				Dim copyProfile As CopyProfile = New CopyProfile(lbxAvailableProfiles.SelectedItem.ToString())
				Dim dgResult As DialogResult = copyProfile.ShowDialog()
                If dgResult = Windows.Forms.DialogResult.OK Then
                    Try
                        mapiManager.CopyProfile(lbxAvailableProfiles.SelectedItem.ToString(), copyProfile.TbxBoxValue.ToString())
                        RenderAllProfilesIntoListBox()
                        RenderAllProfilesIntoComboBox()
                        copyProfile.Dispose()
                    Catch ex As MapiManagerException
                        MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
                    Catch ex As ArgumentNullException
                        MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
                    Catch ex As ArgumentException
                        MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
                    Catch ex As Exception
                        MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
                    End Try
                ElseIf dgResult = Windows.Forms.DialogResult.Cancel Then
                    copyProfile.Dispose()
                End If
			Else
				MessageBox.Show("Please select a profile to copy.", "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End If
		End Sub
		Private Sub btnApplyProfile_Click(ByVal sender As Object, ByVal e As EventArgs)
			SetProfile()
			btnCancelProfile.Enabled = False
			btnApplyProfile.Enabled = False
		End Sub
		Private Sub btnRemove_Click(ByVal sender As Object, ByVal e As EventArgs)
			Try
				mapiManager.DeleteProfile(lbxAvailableProfiles.SelectedItem.ToString())
				RenderAllProfilesIntoListBox()
				RenderAllProfilesIntoComboBox()
			Catch ex As MapiManagerException
				MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As ArgumentNullException
				MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As ArgumentException
				MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As Exception
				MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		Private Sub cmbxGetAllProfiles_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
			Me.btnApplyProfile.Enabled = True
		End Sub
		Private Sub ProfileSettings_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
			If e.KeyCode = Keys.Escape Then
				Me.Close()
			End If
		End Sub
		Private Sub btnCancelProfile_Click(ByVal sender As Object, ByVal e As EventArgs)
			Me.Close()
		End Sub
		Private Sub btnOkProfile_Click(ByVal sender As Object, ByVal e As EventArgs)
			SetProfile()
			Me.Close()
		End Sub
		#End Region
		Private Sub btnRename_Click(ByVal sender As Object, ByVal e As EventArgs)
			If lbxAvailableProfiles.SelectedItem IsNot Nothing Then
				Dim renameProfile As RenameProfile = New RenameProfile(lbxAvailableProfiles.SelectedItem.ToString())
				Dim dgResult As DialogResult = renameProfile.ShowDialog()
                If dgResult = Windows.Forms.DialogResult.OK Then
                    Try
                        mapiManager.RenameProfile(lbxAvailableProfiles.SelectedItem.ToString(), renameProfile.TbxBoxValue.ToString())
                        RenderAllProfilesIntoListBox()
                        RenderAllProfilesIntoComboBox()
                        renameProfile.Dispose()
                    Catch ex As MapiManagerException
                        MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
                    Catch ex As ArgumentNullException
                        MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
                    Catch ex As ArgumentException
                        MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
                    Catch ex As Exception
                        MessageBox.Show(ex.Message, "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
                    End Try
                ElseIf dgResult = Windows.Forms.DialogResult.Cancel Then
                    renameProfile.Dispose()
                End If
			Else
				MessageBox.Show("Please select a profile to rename.", "MAPI Management", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End If
		End Sub
	End Class
End Namespace
